﻿using Nancy.Hosting.Self;
using System;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace SAP.BusinessOne.Cockpit.Owin
{
    public class CockpitServer
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static NancyHost host;

        public static int Start(string pwzArgument)
        {
            try
            {
                host = new NancyHost(Startup.Configure(), new Uri(pwzArgument));
                host.Start();
                log.InfoFormat("Cockpit server started, root url={0}", pwzArgument);
            }
            catch (Exception e)
            {
                log.Error("Failed to start cockpit local server", e);
                return 1;
            }
            return 0;
        }

        public static int Stop(string pwzArgument)
        {
            try
            {
                host.Stop();
                log.Info("Cockpit server stopped.");
            }
            catch (Exception e)
            {
                log.Error("Failed to stop cockpit local server", e);
                return 1;
            }
            return 0;
        }

        public static int GetAvailablePort(string pwzArgument)
        {
            // TODO:
            return 50000;
        }
    }
}
